package com.example.bookkeeping_book.service;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.bookkeeping_book.R;
import com.example.bookkeeping_book.utility.MyFragment;

public class AddActivity extends AppCompatActivity  {
    private ImageView back;
    private TextView chooseExpend;
    private TextView chooseIncome;
//    private TextView setAmount;
//    private TextView setConsumptionItem;
//    private TextView setConsumer;
//    private Button[] bs;
//    private Button ok;
//    private EditText setRemarks;
//
    private int flag = 1;       //当前选择记入支出还是收入
//
//    private String amount = ""; //金额
//    private String[] info;      //消费项目列表
//    private int chooseTid = 0;  //选择消费项目

//    private ArrayList<Consumer> consumers;  //用户列表
    private String consumer_str[];  //用户姓名列表
//    private int chooseCid = 0;  //选择用户列表
    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_activity);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        init();
    }

    private void init() {
        back = findViewById(R.id.iv_back);

//        setRemarks = findViewById(R.id.edt_setRemarks);

        chooseExpend = findViewById(R.id.tv_chooseExpend);
        chooseIncome = findViewById(R.id.tv_chooseIncome);
//        setAmount = findViewById(R.id.tv_setAmount);
//        setConsumptionItem = findViewById(R.id.tv_setConsumptionItem);
//        setConsumer = findViewById(R.id.tv_setConsumer);
//
//        ok = findViewById(R.id.btn_ok);
//        bs = new Button[]{findViewById(R.id.btn_0), findViewById(R.id.btn_1), findViewById(R.id.btn_2), findViewById(R.id.btn_3), findViewById(R.id.btn_4), findViewById(R.id.btn_5), findViewById(R.id.btn_6), findViewById(R.id.btn_7), findViewById(R.id.btn_8), findViewById(R.id.btn_9), findViewById(R.id.btn_del), findViewById(R.id.btn_point)};
//        for (Button a : bs) {
//            a.setOnClickListener(this);
//        }
//
//        getAllConsumptionItem(R.raw.item1);
//        getAllConsumer();

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        chooseExpend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                chooseExpend.setBackground(getDrawable(R.drawable.font_line));
                chooseIncome.setBackground(null);
//                getAllConsumptionItem(R.raw.item1);
//                setConsumptionItem.setText(info[0]);
//                setAmount.setText("0.00");
//                amount = "";
                flag = 1;
                fragmentManager = getSupportFragmentManager();
                fragmentTransaction = fragmentManager.beginTransaction();
                MyFragment fragment = new MyFragment(1,AddActivity.this);
                fragmentTransaction.replace(R.id.fv,fragment);
                fragmentTransaction.commit();

            }
        });

        chooseIncome.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                chooseExpend.setBackground(null);
                chooseIncome.setBackground(getDrawable(R.drawable.font_line));
//                getAllConsumptionItem(R.raw.item2);
//                setConsumptionItem.setText(info[0]);
//                setAmount.setText("0.00");
//                amount = "";
                flag = 0;
                fragmentManager = getSupportFragmentManager();
                fragmentTransaction = fragmentManager.beginTransaction();
                MyFragment fragment = new MyFragment(0,AddActivity.this);
                fragmentTransaction.replace(R.id.fv,fragment);
                fragmentTransaction.commit();
            }
        });
        fragmentManager = getSupportFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        MyFragment fragment = new MyFragment(1,this);
        fragmentTransaction.replace(R.id.fv,fragment);
        fragmentTransaction.commit();

//        ok.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                double num_amount = Double.parseDouble(setAmount.getText().toString());
//                if (flag == 1)
//                    num_amount = -num_amount;
//                AccountBook accountBook = new AccountBook(consumers.get(chooseCid).getId(), setConsumptionItem.getText().toString(), num_amount, setRemarks.getText().toString());
//                Utility.insert(new MySqlite(AddActivity.this), accountBook);
//                Toast.makeText(AddActivity.this, "已计入账单" + num_amount + "元", Toast.LENGTH_SHORT).show();
//                finish();
//            }
//        });
//
//        setConsumptionItem.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                new AlertDialog.Builder(AddActivity.this)
//                        .setTitle("请选择")
//                        .setSingleChoiceItems(info, chooseTid, new DialogInterface.OnClickListener() {
//                            @Override
//                            public void onClick(DialogInterface dialogInterface, int i) {
//                                chooseTid = i;
//                            }
//                        })
//                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
//                            @Override
//                            public void onClick(DialogInterface dialogInterface, int i) {
//                                setConsumptionItem.setText(info[chooseTid]);
//                            }
//                        })
//                        .show();
//            }
//        });
//
//        setConsumer.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                getAllConsumer();
//                new AlertDialog.Builder(AddActivity.this)
//                        .setTitle("请选择")
//                        .setSingleChoiceItems(consumer_str, chooseCid, new DialogInterface.OnClickListener() {
//                            @Override
//                            public void onClick(DialogInterface dialogInterface, int i) {
//                                chooseCid = i;
//                            }
//                        })
//                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
//                            @Override
//                            public void onClick(DialogInterface dialogInterface, int i) {
//                                setConsumer.setText(consumers.get(chooseCid).getName());
//                            }
//                        })
//                        .setNeutralButton("删除", new DialogInterface.OnClickListener() {
//                            @Override
//                            public void onClick(DialogInterface dialogInterface, int i) {
//                                androidx.appcompat.app.AlertDialog dialog;
//                                dialog = new androidx.appcompat.app.AlertDialog.Builder(AddActivity.this)
//                                        .setTitle("提示")
//                                        .setMessage("是否删除")
//                                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
//                                            @Override
//                                            public void onClick(DialogInterface dialogInterface, int i) {
//                                                if (Utility.deleteConsumer(new MySqlite(AddActivity.this), consumers.get(chooseCid).getId())) {
//                                                    Toast.makeText(AddActivity.this, "已删除该用户", Toast.LENGTH_SHORT).show();
//                                                    chooseCid = 0;
//                                                } else {
//                                                    Toast.makeText(AddActivity.this, "不可以删除当前用户", Toast.LENGTH_SHORT).show();
//
//                                                }
//                                            }
//                                        })
//                                        .setNegativeButton("取消", null)
//                                        .create();
//                                dialog.show();
//
//                            }
//                        })
//                        .setNegativeButton("添加", new DialogInterface.OnClickListener() {
//                            @Override
//                            public void onClick(DialogInterface dialogInterface, int i) {
//                                MyDialog myDialog = new MyDialog(AddActivity.this);
//                                myDialog.setTitle("添加消费用户");
//                                myDialog.setAddOnclickListener("添加", new MyDialog.onAddOnclickListener() {
//                                    @Override
//                                    public void onAddClick() {
//                                        if (!myDialog.getEditText().equals(""))
//////                    Log.i(editName.getText().toString(), "onClick: ");
//                                            Utility.insert(new MySqlite(AddActivity.this), myDialog.getEditText());
//                                        myDialog.dismiss();
//                                    }
//                                });
//                                myDialog.show();
//                            }
//                        })
//                        .show();
//            }
//        });

    }

//    private void getAllConsumptionItem(int id) {
//        InputStream is = this.getResources().openRawResource(id);
//        try {
//            List<String> list = Utility.getInfo(is);
//            info = list.toArray(new String[list.size()]);
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
//    }
//
//    private void getAllConsumer() {
//        consumers = Utility.selectAllConsumer(new MySqlite(AddActivity.this));
//        consumer_str = new String[consumers.size()];
//        for (int i = 0; i < consumers.size(); i++) {
//            consumer_str[i] = consumers.get(i).getName();
//        }
//    }

//    @Override
//    public void onClick(View view) {
//        switch (view.getId()) {
//            case R.id.btn_0:
//            case R.id.btn_1:
//            case R.id.btn_2:
//            case R.id.btn_3:
//            case R.id.btn_4:
//            case R.id.btn_5:
//            case R.id.btn_6:
//            case R.id.btn_7:
//            case R.id.btn_8:
//            case R.id.btn_9:
//                if (amount.length() >= 10)
//                    break;
//                if (amount.indexOf(".") > 0 && amount.indexOf(".") + 3 == amount.length())
//                    break;
//                Button button = findViewById(view.getId());
//                if (amount.equals("0"))
//                    amount = button.getText().toString();
//                else
//                    amount += button.getText().toString();
//                break;
//            case R.id.btn_point:
//                if (amount.length() >= 10)
//                    break;
//                if (amount.indexOf(".") < 0)
//                    amount += ".";
//                if (amount.length() == 0)
//                    amount = "0.";
//                break;
//            case R.id.btn_del:
//                amount = amount.substring(0, amount.length() - 1);
//                if (amount.length() == 0)
//                    amount = "0";
//                break;
//        }
//        setAmount.setText(amount);
//    }
}